import json

import scrapy

from ..items import XingzhengquhuaItem, XingzhengxukegonggaoItem, XingzhengchufagonggaoItem


# 行政区域
class GetXinagzhengquhuaSpider(scrapy.Spider):
    name = "xzqh"
    allowed_domains = ["www.mca.gov.cn"]
    start_urls = ["https://www.mca.gov.cn/mzsj/xzqh/2022/202201xzqh.html"]

    def parse(self, response):
        list = response.xpath('//tr[position() > 3]')
        for each in list:
            data = each.xpath(".//td/text()").extract()
            item = XingzhengquhuaItem()
            item['id'] = data[0]
            item['name'] = data[1]
            yield item

# 行政许可公告
class XinagzhengxukegonggaoSpider(scrapy.Spider):
    name = "xzxkgg"
    allowed_domains = ["chinanpo.mca.gov.cn"]
    # start_urls = ["https://chinanpo.mca.gov.cn/selectXzxkgg?pageNum=1&pageSize=10"]

    page = 0
    page_size = 1000
    # 准备要发送的数据
    data = {"orgName": "", "unifiedCode": "", "catalog": ""}
    # 将数据转换为JSON格式
    json_data = json.dumps(data)
    def get_next_page(self):
        self.page += 1
        return self.page

    def send_requests(self):
        page = self.get_next_page()
        url = f'https://chinanpo.mca.gov.cn/selectXzxkgg?pageNum={page}&pageSize={self.page_size}'
        # 创建一个新的请求对象，设置URL、方法为POST，内容类型为application/json，并将JSON数据作为body传递
        return scrapy.Request(
            url=url,
            method='POST',
            headers={'Content-Type': 'application/json'},
            body=self.json_data,
            callback=self.parse
        )

    def start_requests(self):
        yield self.send_requests()

    def parse(self, response):
        data = response.json()
        total = data['total']
        rows = data['rows']
        for row in rows:
            item = XingzhengxukegonggaoItem()
            item['orgName'] = row['orgName']
            item['unifiedCode'] = row['unifiedCode']
            item['nodeName'] = row['nodeName']
            item['modifyBefore'] = row['modifyBefore']
            item['modifyAfter'] = row['modifyAfter']
            item['legalName'] = row['legalName']
            item['approveTime'] = row['approveTime']
            yield item
        if total > self.page * self.page_size:
            yield self.send_requests()


# 行政处罚公告
class XinagzhengchufagonggaoSpider(scrapy.Spider):
    name = "xzcfgg"
    allowed_domains = ["chinanpo.mca.gov.cn"]

    page = 0
    page_size = 1000
    # 准备要发送的数据
    data = {"orgName": "", "usci": ""}
    # 将数据转换为JSON格式
    json_data = json.dumps(data)
    def get_next_page(self):
        self.page += 1
        return self.page

    def send_requests(self):
        page = self.get_next_page()
        url = f'https://chinanpo.mca.gov.cn/selectXzcfgg?pageNum={page}&pageSize={self.page_size}'
        # 创建一个新的请求对象，设置URL、方法为POST，内容类型为application/json，并将JSON数据作为body传递
        return scrapy.Request(
            url=url,
            method='POST',
            headers={'Content-Type': 'application/json'},
            body=self.json_data,
            callback=self.parse
        )

    def start_requests(self):
        yield self.send_requests()

    def parse(self, response):
        data = response.json()
        total = data['total']
        rows = data['rows']
        for row in rows:
            item = XingzhengchufagonggaoItem()
            item['orgName'] = row['orgName']
            item['usci'] = row['usci']
            item['caseNo'] = row['caseNo']
            item['punishName'] = row['punishName']
            item['punishTime'] = row['punishTime']
            item['punishBasis'] = row['punishBasis']
            item['punishReason'] = row['punishReason']
            item['punishOrgan'] = row['punishOrgan']
            yield item
        if total > self.page * self.page_size:
            yield self.send_requests()

